package org.example;

public class Test1 {
    //leetcode 35 搜索插入位置 https://leetcode.cn/problems/search-insert-position/description/
    public int searchInsert(int[] nums, int target) {
        //将数组分为两个部分：小于目标值的部分和大于等于目标值的部分
        int n = nums.length;
        int l = 0, r = n - 1, mid = 0;
        while (l < r) {
            mid =  (l + r) / 2;
            if (nums[mid] < target) {
                l = mid + 1;
            }else {
                r = mid;
            }
        }
        if (nums[l] == target) return l;
        else if (nums[l] > target) return l;
        else return l + 1;
    }
}
